/*
 * xAPBSCtext.java
 *
 * Java class used to manage xAP BSC text information
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the canTracer project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 11/10/09 by Daniel Berenguer : first version
 */

package xAP;


/********************************************************************************************
* xAPBSCtext
*
* xAP BSC text class
*
********************************************************************************************/
public class xAPBSCtext
{
    // Member variables
    private String text;

   /********************************************************************************************
    * xAPBSCtext
    *
    * xAP BSC text constructor
    *
    * 'strText'    xAP BSC text string
    *
    ********************************************************************************************/
    public xAPBSCtext(String strText)
    {
        text = strText;
    }

   /********************************************************************************************
    * textToDouble
    *
    * Convert BSC text into double value
    *
    ********************************************************************************************/
    public double toDouble()
    {
        double dblVal = 0.0;
        int i;

        for(i=0 ; i<text.length() ; i++)
        {
            if (!isNumberDot(text.charAt(i)))
                break;
        }

        if (i > 0)
            dblVal = Double.parseDouble(text.substring(0, i));

        return dblVal;
    }

   /********************************************************************************************
    * textToInteger
    *
    * Convert BSC text into integer value
    *
    ********************************************************************************************/
    public int toInteger()
    {
        int intVal = 0;
        int i;

        for(i=0 ; i<text.length() ; i++)
        {
            if (!isNumber(text.charAt(i)))
                break;
        }

        if (i > 0)
            intVal = Integer.parseInt(text.substring(0, i));

        return intVal;
    }

   /********************************************************************************************
    * textToByte
    *
    * Convert BSC text into a byte value
    *
    ********************************************************************************************/
    public byte toByte()
    {
        byte byteVal = 0;
        int i;

        for(i=0 ; i<text.length() ; i++)
        {
            if (!isNumber(text.charAt(i)))
                break;
        }

        if (i > 0)
            byteVal = Byte.parseByte(text.substring(0, i));

        return byteVal;
    }

   /********************************************************************************************
    * textToString
    *
    * Convert BSC text into a string
    *
    ********************************************************************************************/
    public String toString()
    {
        return text;
    }

   /********************************************************************************************
    * isNumber
    *
    * Returns true if the character passed as argument is a 0-9 digit
    *
    ********************************************************************************************/
    private boolean isNumber(char a)
    {
        if (a < 0x30 || a > 0x39)
            return false;
        return true;
    }

   /********************************************************************************************
    * isNumberDot
    *
    * Returns true if the character passed as argument is a 0-9 digit or one of the following
    * characters: "+", "-", ".", ","
    *
    ********************************************************************************************/
    private boolean isNumberDot(char a)
    {
        if (a < 0x2B || a > 0x39 || a == 0x2F)
            return false;
        return true;
    }
}
